package com.example.hello.controller;

import com.example.hello.common.PageResult;
import com.example.hello.common.Result;
import com.example.hello.entity.Dept;
import com.example.hello.service.DeptService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {

    @Autowired
    private DeptService deptService;

    /**
     * 分页查询部门
     */
    @GetMapping("/page")
    public Result page(@RequestParam(defaultValue = "") String name,
                      @RequestParam(defaultValue = "1") Integer page,
                      @RequestParam(defaultValue = "10") Integer pageSize) {
        PageResult pageResult = deptService.page(name, page, pageSize);
        return Result.success(pageResult);
    }

    /**
     * 查询所有部门
     */
    @GetMapping("/list")
    public Result list() {
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }

    /**
     * 根据ID查询部门
     */
    @GetMapping("/{id}")
    public Result getById(@PathVariable Integer id) {
        Dept dept = deptService.getById(id);
        return Result.success(dept);
    }

    /**
     * 新增部门
     */
    @PostMapping
    public Result add(@RequestBody Dept dept) {
        deptService.add(dept);
        return Result.success("新增成功");
    }

    /**
     * 更新部门
     */
    @PutMapping
    public Result update(@RequestBody Dept dept) {
        deptService.update(dept);
        return Result.success("更新成功");
    }

    /**
     * 根据ID删除部门
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id) {
        deptService.deleteById(id);
        return Result.success("删除成功");
    }

    /**
     * 查询所有部门
     */
    @GetMapping
    public Result listAll() {
        List<Dept> deptList = deptService.list();
        return Result.success(deptList);
    }
} 